package cn.itcast.wanxinp2p.account.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * <p>
 * Swagger 配置类，Swagger可以方便的帮助Java程序员生成接口文档
 * 注解：@Configuration 声明档当前类是配置类
 * 注解：@ConditionalOnProperty 条件加载机制，也就是当发现 swagger.enable=true 时候才加载这个配置类
 * 注解：@EnableSwagger2  开启swagger功能，也就是可以基于swagger注解生成接口文档
 * </p>
 *
 * @author <a href=mailto:zhaoboying@itcast.cn> byZhao </a>
 * @version 1.0.0
 * @motto Talk is cheap, show me the code!
 * @since 2022-03-24 11:20:48
 */
@Configuration
@ConditionalOnProperty(prefix = "swagger", value = {"enable"}, havingValue = "true")
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInfo())
                .select()
                // 要扫描的API(Controller)基础包
                .apis(RequestHandlerSelectors.basePackage("cn.itcast.wanxinp2p"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo buildApiInfo() {
        Contact contact = new Contact("黑马程序员", "", "");
        return new ApiInfoBuilder()
                .title("万信金融P2P平台-用户服务API文档")
                .description("包含用户服务api")
                .contact(contact)
                .version("1.0.0").build();
    }
}
